package important.demo;

import java.util.regex.Pattern;

import passengers.demo.NewPassMainActivity;
import presence.demo.R;
import database.demo.ImportantOperations;
import database.demo.PassengersOperations;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * saving important contact details
 */
public class NewImportantMainActivity extends Activity {
	private ImportantOperations importantDBoperation;
	TextView tmpID;
	EditText tmpName;
	EditText tmpRole;
	EditText tmpPhone;
	EditText tmpEmail;
	EditText tmpAddress;
	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
            "[a-zA-Z0-9+._%-+]{1,256}" +
            "@" +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
            "(" +
            "." +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
            ")+"
        );
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_important);	
		importantDBoperation = new ImportantOperations(this);
		importantDBoperation.open();
		getMaxColumnData();
		addButtonListeners();
		findViewById(R.id.new_impo_name).requestFocus();
	}
	
	
	public void getMaxColumnData() {
		tmpID=(TextView) findViewById(R.id.idTextt);
		Cursor cursor = importantDBoperation.getMaxID();
		if(cursor.getCount()>0){
			int tmp= Integer.parseInt(cursor.getString(0));
			if (tmp>=1){
				tmp++;
				tmpID.setText(tmp+"");
			}
		}
		else tmpID.setText("1");
	}
	
	
	public void addButtonListeners() {	 
		Button addButtton = (Button) findViewById(R.id.btnAddPass);
		tmpID = (TextView) findViewById(R.id.idTextt);
		tmpName = (EditText) findViewById(R.id.new_impo_name);
		tmpRole = (EditText) findViewById(R.id.new_impo_role);
		tmpPhone = (EditText) findViewById(R.id.new_impo_phone);
		tmpEmail = (EditText) findViewById(R.id.new_impo_mail);
		tmpAddress = (EditText) findViewById(R.id.new_impo_address);
		addButtton.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View view) {
			if(tmpName.getText().toString().length()==0)
			{
				Toast.makeText(NewImportantMainActivity.this,"Please enter name", Toast.LENGTH_LONG).show();
			}
			else if(checkEmail(tmpEmail.getText().toString()))
            {	
				addImportant(tmpID,tmpName,tmpRole,tmpPhone,tmpEmail, tmpAddress);	
            }
            else
            {
               Toast.makeText(NewImportantMainActivity.this,"Invalid Email Addresss", Toast.LENGTH_LONG).show();
            }	
		}
		}); 		
	}
	
	private boolean checkEmail(String email) {
		if(email.length()==0){
			return true;
		}
		else return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }
	
	public void addImportant(TextView tmpID,EditText tmpName, EditText tmpRole, EditText tmpPhone, EditText tmpEmail, EditText tmpAddress) {
			
		if(importantDBoperation.addImportantContant(tmpID.getText().toString(), tmpName.getText().toString(),
				tmpRole.getText().toString(), tmpPhone.getText().toString(),tmpEmail.getText().toString(), 
				tmpAddress.getText().toString())==null)
		{
			Toast.makeText(NewImportantMainActivity.this,"ID already exists!", Toast.LENGTH_LONG).show();
		}
		else{
		
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(":)");
		alertDialog.setMessage(tmpName.getText()+" added successfully! ");
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			NewImportantMainActivity.this.finish();
		}
		});
		alertDialog.setIcon(R.drawable.ic_launcher);
		alertDialog.show();
		}
	}
}












